﻿/*  ----------------------------------------------------------------------------
 *  Author:
 *  Chandra Shekhar Tripathi
 *  ----------------------------------------------------------------------------
 *  Product Name:
 *  DbDocumenter
 *  ----------------------------------------------------------------------------
 *  Year:
 *  2009
 *  ----------------------------------------------------------------------------
 *  File:
 *  UtilityManager.cs
 *  ----------------------------------------------------------------------------
 */
namespace DBDocumenter.BLL.Services
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.XPath;
    using System.Xml.Xsl;

    using DBDocumenter.BLL.Entities;
    using DBDocumenter.BLL.Services.HTML;
    using DBDocumenter.Exception;
    using DBDocumenter.Helper;
    using DBDocumenter.Resources;

    #region Enumerations

    public enum Folders
    {
        HTML,
        XML
    }

    #endregion Enumerations

    /// <summary>
    /// 
    /// </summary>
    public class UtilityManager
    {
        #region Fields

        private static UtilityManager instance = new UtilityManager();

        XDocument document = null;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UtilityManager"/> class.
        /// </summary>
        private UtilityManager()
        {
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static UtilityManager Instance
        {
            get { return UtilityManager.instance; }
            set { UtilityManager.instance = value; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Clears the folder.
        /// </summary>
        /// <param name="folderName">Name of the folder.</param>
        public static void ClearFolder(Folders folderName)
        {
            try
            {
                string location = Directory.GetCurrentDirectory();
                string currentLocation = string.Empty;

                //List<string> extensions = new List<string>();

                currentLocation = location + "\\" + folderName;
                foreach (string file in System.IO.Directory.GetFiles(currentLocation))
                {
                    File.Delete(file);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Error, ex.ToString());
            }
        }

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <returns></returns>
        public string GetConnectionString()
        {
            return Utility.connectionString;
        }

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <returns></returns>
        public string GetConnectionString(string database)
        {
            string conString = Utility.connectionString + "Database = " + database + ";";
            return conString;
        }

        /// <summary>
        /// Gets the setting.
        /// </summary>
        /// <returns></returns>
        public Setting GetSetting()
        {
            Setting result = new Setting();

            result.CurrentTheme = GetSetting(BLLResources.ThemeFile);

            return result;
        }

        /// <summary>
        /// Loads the settings.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        public void LoadSettings(string fileName)
        {
            document = XDocument.Load(fileName);
        }

        /// <summary>
        /// Sets the connection string.
        /// </summary>
        /// <param name="serverName">Name of the server.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="pwd">The PWD.</param>
        /// <param name="trustedConnection">The trusted connection.</param>
        public void SetConnectionString(string serverName, string userId, string pwd, string trustedConnection)
        {
            Utility.connectionString = serverName + userId + pwd + trustedConnection;
        }

        /// <summary>
        /// Sets the settings.
        /// </summary>
        /// <param name="value">The value.</param>
        public void SetSettings(Setting value)
        {
            SetSetting(BLLResources.ThemeFile, value.CurrentTheme);
        }

        /// <summary>
        /// Writes the HTML help.
        /// </summary>
        public void WriteHTMLHelp()
        {
            try
            {
                this.CreateTreeMenu();
                string sourcePath, destinationPath;
                sourcePath = Directory.GetCurrentDirectory();
                destinationPath = Utility.fileLocation;

                sourcePath = sourcePath.EndsWith(@"\") ? sourcePath : sourcePath + @"\";
                destinationPath = destinationPath.EndsWith(@"\") ? destinationPath : destinationPath + @"\";
                sourcePath = sourcePath + @"HTML\";
                destinationPath = destinationPath + Utility.databaseName + @"\HTML\";
                HTML.HTMLManager.CopyDirectory(sourcePath, destinationPath, true);

                sourcePath = Directory.GetCurrentDirectory();
                destinationPath = Utility.fileLocation;
                sourcePath = sourcePath.EndsWith(@"\") ? sourcePath : sourcePath + @"\";
                destinationPath = destinationPath.EndsWith(@"\") ? destinationPath : destinationPath + @"\";
                if (!File.Exists(destinationPath + Utility.databaseName + @"\index.htm"))
                {
                    File.Create(destinationPath + Utility.databaseName + @"\index.htm").Close();
                }
                File.Copy(sourcePath + "index.htm", destinationPath + Utility.databaseName + @"\index.htm", true);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(LogType.BusinessLayer, LogLevel.Error, ex.ToString());
            }
        }

        /// <summary>
        /// Creates the tree menu.
        /// </summary>
        void CreateTreeMenu()
        {
            XMLManager.WriteXML("Tree", XMLManager.GetTreeMenuXML());
            HTMLManager.WriteHTML("Tree.xml", "TreeMenu.xsl", "Tree.htm");
        }

        /// <summary>
        /// Gets the setting.
        /// </summary>
        /// <param name="xPath">The x path.</param>
        /// <returns></returns>
        string GetSetting(string xPath)
        {
            if (this.document != null)
            {
                XElement element = this.document.XPathSelectElement(xPath);

                if (element != null)
                {
                    return element.Value.Trim();
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Sets the setting.
        /// </summary>
        /// <param name="xPath">The x path.</param>
        /// <param name="value">The value.</param>
        void SetSetting(string xPath, string value)
        {
            if (this.document != null)
            {
                XElement element = this.document.XPathSelectElement(xPath);

                if (element != null)
                {
                    element.SetValue(value);
                    this.document.Save("Settings.xml");
                }
            }
        }

        #endregion Methods

        #region Other

        ///// <summary>
        ///// method for reading a value from a resource file
        ///// (.resx file)
        ///// </summary>
        ///// <param name="file">file to read from</param>
        ///// <param name="key">key to get the value for</param>
        ///// <returns>a string value</returns>
        //public object ReadResourceValue(string key)
        //{
        //    //value for our return value
        //    object resourceValue = new object();
        //    try
        //    {
        //        //retrieve the value of the specified key
        //        resourceValue = Properties.Resources.ResourceManager.GetObject(key);
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.Message);
        //        resourceValue = string.Empty;
        //    }
        //    return resourceValue;
        //}

        #endregion Other
    }
}